﻿Public Class ItemsVenta2 

    Private Sub ItemsVenta2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Textventa.Text = My.Forms.ItemsVenta.IditemventaTextEdit.Text
        Try
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventa' table. You can move, or remove it, as needed.
            Me.ItemventaTableAdapter.Fill(Me.DbFireAndIceDataSet.itemventa)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.productos' table. You can move, or remove it, as needed.
            Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
            'TODO: This line of code loads data into the 'DbFireAndIceDataSet.itemventadetalle' table. You can move, or remove it, as needed.
            MsgBox(Textventa.Text)
            Me.ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, Val(Textventa.Text))
        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar registros de la Base de Datos")
        End Try
        CostototalTextBox.EditValue = 0
        CostounitarioTextBox.EditValue = 0
        PrecioventaexternoTextBox.EditValue = 0
        PrecioventaTextBox.EditValue = 0
        TotallineaTextBox.EditValue = 0
        CantidadTextBox.EditValue = 0
        RadioButton1.Checked = True

    End Sub

    Private Sub IdproductoTextEdit_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdproductoTextEdit.EditValueChanged
        IdproductoTextBox.EditValue = IdproductoTextEdit.GetColumnValue("idproducto")
        Descripcion.EditValue = IdproductoTextEdit.GetColumnValue("descripcion")
        CostounitarioTextBox.EditValue = IdproductoTextEdit.GetColumnValue("preciocomprauni")
        'IdunidadTextBox.EditValue = IdproductoTextEdit.GetColumnValue("idunidad")
        'CantidadTextBox.EditValue = 0
        TotallineaTextBox.EditValue = 0
        PrecioventaexternoTextBox.EditValue = 0
        PrecioventaTextBox.EditValue = 0
        RadioButton1.Checked = True
    End Sub

    Private Sub IdventaTextEdit1_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdventaTextEdit1.EditValueChanged
        Dim codigo As Integer
        Try
            codigo = IdventaTextEdit1.GetColumnValue("iditemventa")
            Descripcion.EditValue = IdventaTextEdit1.GetColumnValue("descripcion")

            Me.ItemventadetalleTableAdapter.FillQueryItemdetalle(Me.DbFireAndIceDataSet.itemventadetalle, codigo)

        Catch ex As System.Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar los datos de Items")
            'System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub AgregarItemDetalle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AgregarItemDetalle.Click
        If RadioButton2.Checked = True Then
            Dim iditem As Integer
            Dim idpro As Double
            Dim canti As Double

            Dim costu As Double
            Dim tota As Double

            For i As Integer = 0 To GridView2.DataRowCount - 1

                'ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, Textventa.EditValue)


                iditem = GridView2.GetRowCellValue(i, "iditemventa")
                idpro = GridView2.GetRowCellValue(i, "idproducto")

                costu = GridView2.GetRowCellValue(i, "costounitario")
                tota = GridView2.GetRowCellValue(i, "totallinea")
                canti = GridView2.GetRowCellValue(i, "cantidad")
                'tota = costu * canti
                Try
                    ItemventadetalleTableAdapter.InsertQueryItemdetalle(Textventa.EditValue, idpro, canti, costu, tota)
                    'ItemventadetalleTableAdapter.Fill(DbFireAndIceDataSet.itemventadetalle)
                Catch ex As Exception
                    AlertControl1.Show(Owner, "ERROR", "No se pudo insertar el encabezado")
                End Try
                ' ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(IditemventaTextEdit.Text))
            Next i
        Else
            If CantidadTextBox.Text = 0 Then
                ErrorProvider1.SetError(CantidadTextBox, "Debe ingresar un nombre para el Documento")
            Else



                ErrorProvider1.SetError(CantidadTextBox, Nothing)
                ' Try

                TotallineaTextBox.EditValue = CDbl(CantidadTextBox.EditValue) * CDbl(CostounitarioTextBox.EditValue)

                'Dim existe As Boolean = False
                'Dim total As Decimal
                ' Dim cantidad As Decimal
                'Dim colum As Integer

                '  If GridView1.RowCount = 0 Then
                'MsgBox("vacio")

                
                CostototalTextBox.EditValue = CDbl(TotallineaTextBox.EditValue) + CDbl(CostototalTextBox.EditValue)
                'MsgBox(CostototalTextBox.Text)
                Try
                    ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(Textventa.EditValue), Val(IdproductoTextBox.EditValue), Val(CantidadTextBox.EditValue), Val(CostounitarioTextBox.EditValue), Val(TotallineaTextBox.EditValue))
                    ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, Textventa.EditValue)
                    ' Else
                Catch ex As Exception
                    AlertControl1.Show(Owner, "ERROR", "No se pudo guardar el registro")
                End Try

                ' Catch ex As Exception
                'MsgBox("No se pudo almcenar el Item")
                ' End Try



                'End If
                'Me.ProductosTableAdapter.Fill(Me.DbFireAndIceDataSet.productos)
            End If
        End If
        Try
            ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, Textventa.EditValue)
            CostototalTextBox.EditValue = 0
        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar los datos de Items")
        End Try

        For i As Integer = 0 To GridView2.DataRowCount - 1


            CostototalTextBox.EditValue = CDbl(GridView2.GetRowCellValue(i, "totallinea")) + CDbl(CostototalTextBox.EditValue)


            ' ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(IditemventaTextEdit.Text))
        Next i

        CostounitarioTextBox.EditValue = 0
        PrecioventaexternoTextBox.EditValue = 0
        PrecioventaTextBox.EditValue = 0
        TotallineaTextBox.EditValue = 0
        CantidadTextBox.EditValue = 0
        guardar.Enabled = True
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            RadioButton2.Checked = False
            IdproductoTextEdit.Visible = True
            IdventaTextEdit1.Visible = False
            Label1.Text = "Busqueda de Productos"
            Descripcion.Properties.ReadOnly = False
            CantidadTextBox.Properties.ReadOnly = False

            CostounitarioTextBox.Properties.ReadOnly = True
            CantidadTextBox.Properties.ReadOnly = False
            ' IdunidadTextBox.Properties.ReadOnly = True
            PrecioventaTextBox.Properties.ReadOnly = False
            PrecioventaexternoTextBox.Properties.ReadOnly = False
            CostototalTextBox.Properties.ReadOnly = True
            TotallineaTextBox.Properties.ReadOnly = True
            PrecioventaexternoTextBox.EditValue = 0
            PrecioventaTextBox.EditValue = 0
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked = True Then
            RadioButton1.Checked = False

            IdproductoTextEdit.Visible = False
            IdventaTextEdit1.Visible = True
            Label1.Text = "Busqueda de Items"
            CantidadTextBox.Properties.ReadOnly = True

            'IdunidadTextBox.Properties.ReadOnly = True

            TotallineaTextBox.Properties.ReadOnly = True
            CostototalTextBox.Properties.ReadOnly = True
            CostounitarioTextBox.Properties.ReadOnly = True
            Descripcion.Properties.ReadOnly = True
        End If
    End Sub

    Private Sub CantidadTextBox_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CantidadTextBox.EditValueChanged
        TotallineaTextBox.EditValue = Val(CantidadTextBox.EditValue) * Val(CostounitarioTextBox.EditValue)
    End Sub

    Private Sub guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles guardar.Click
        Try
            ItemventadetalleTableAdapter.FillQueryItemdetalle(DbFireAndIceDataSet.itemventadetalle, Textventa.EditValue)
            My.Forms.ItemsVenta.CostototalTextBox.EditValue = 0
        Catch ex As Exception
            AlertControl1.Show(Owner, "ERROR", "No se pudo cargar los datos de Items")
        End Try

        For i As Integer = 0 To GridView2.DataRowCount - 1


            My.Forms.ItemsVenta.CostototalTextBox.EditValue = GridView2.GetRowCellValue(i, "totallinea") + My.Forms.ItemsVenta.CostototalTextBox.EditValue


            ' ItemventadetalleTableAdapter.InsertQueryItemdetalle(Val(IditemventaTextEdit.Text))
        Next i
        Me.Close()
    End Sub
End Class